package ibmetp.mis.dao;

import ibmetp.mis.vo.Page;
import ibmetp.mis.vo.Role;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class RoleDao extends BaseDao {

	@Override
	/**
	 * 将查询结果转换成role类集，并保存进List集合中
	 * @author  LIN A ER
	 * @create 2011/10/5
	 */
	public List ResultSet2List(ResultSet rs) {
		// TODO Auto-generated method stub
		List<Role> resultList=new ArrayList<Role>();
		try {
			while(rs!=null&&rs.next())
			{
				Role role=new Role();
				role.setRoleId(rs.getLong("ROLE_ID"));
				role.setRoleName(rs.getString("ROLE_NAME"));
				role.setDescript(rs.getString("DESCRIPT"));
				resultList.add(role);
			}
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultList;
	}

	@Override
	/**
	 * 添加查询条件
	 */
	public Map<Integer, Object[]> getCondition(Object o) {
		// TODO Auto-generated method stub
		Map<Integer,Object[]> paramsMap=new HashMap<Integer,Object[]>();
		Integer count=0;
		Role role=(Role)o;
		if(role.getRoleId()!=null&&role.getRoleId()>0)
		{
			count++;
			paramsMap.put(count, new Object[]{" and ROLE_ID LIKE ?","%"+role.getRoleId()+"%"});
		}
		if(role.getRoleName()!=null&&role.getRoleName().trim().length()>0)
		{
			count++;
			paramsMap.put(count, new Object[]{"and ROLE_NAME LIKE ?","%"+role.getRoleName()+"%"});
		}
		return paramsMap;
	}
	/**
	 * 通过角色id获取角色信息
	 * @param id
	 * @return
	 */
	public Role getRoleById(Long id)
	{
		String sql="select r.* from BAS_T_ROLE r where r.ROLE_ID=?";
		System.out.println(sql);
		conn=DBUtil.getConnection();
		PreparedStatement ps;
		Role role=null;
		UserDao ud=new UserDao();
		try{
			ps=conn.prepareStatement(sql);
			ps.setLong(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs!=null && rs.next())
			{
				role=new Role();
				role.setRoleId(rs.getLong("ROLE_ID"));
				role.setRoleName(rs.getString("ROLE_NAME"));
				role.setDescript(rs.getString("DESCRIPT"));
				role.setCreator(ud.getUserById(rs.getLong("CREATE_BY")));
				role.setCreateDate(rs.getDate("CREATE_DATE"));
				role.setUpdater(ud.getUserById(rs.getLong("UPDATE_BY")));
				role.setUpdateDate(rs.getDate("UPDATE_DATE"));
			}
			ps.close();
			conn.close();			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return role;
	}
	/**
	 * 更新角色信息
	 * @param role
	 */
	public void updateRole(Role role)
	{
		String sql="update BAS_T_ROLE set ROLE_NAME=?,DESCRIPT=? where ROLE_ID=?";
    	System.out.println(sql);
		conn=DBUtil.getConnection();
		PreparedStatement ps;
		
		try{
			ps=conn.prepareStatement(sql);
			ps.setString(1, role.getRoleName());
			ps.setString(2, role.getDescript());
			ps.setLong(3, role.getRoleId());
			
			ps.execute();
			
			ps.close();
			conn.close();
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}		
	}
	/**
	 * 批量添加角色
	 * @param roles
	 */
	public void addRoles(List<Role> roles)
	{
		String sql="insert into BAS_T_ROLE(ROLE_ID,ROLE_NAME,DESCRIPT) values(?,?,?)";
		System.out.println();
		conn=DBUtil.getConnection();
		PreparedStatement ps;
		
		try {
			ps=conn.prepareStatement(sql);
			for(int i=0;i<roles.size();i++)
			{
				Role role=roles.get(i);
				ps.setLong(1, role.getRoleId());
				ps.setString(2, role.getRoleName());
				ps.setString(3, role.getDescript());
				ResultSet rs=ps.executeQuery();
			}
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * 添加新角色
	 * @param role新角色信息
	 */
	public void addRole(Role role)
	{
		String sql="insert into BAS_T_ROLE(ROLE_ID,ROLE_NAME,DESCRIPT) values(ROLE_SEQ.NEXTVAL,?,?)";
		System.out.println();
		conn=DBUtil.getConnection();
		PreparedStatement ps;
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, role.getRoleName());
			ps.setString(2, role.getDescript());
			ResultSet rs = ps.executeQuery();
			ps.close();
			conn.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	/**
	 * 通过角色id信息删除角色
	 * @param id
	 */
	public void deleteById(Long id)
	{
		String sql="delete from BAS_T_ROLE where ROLE_ID=?";
		System.out.println(sql);
		conn=DBUtil.getConnection();
		PreparedStatement ps;
		
		try {
			ps=conn.prepareStatement(sql);
			ps.setLong(1, id);
			ps.execute();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 批量删除角色
	 * @param ids 角色id列表
	 */
	public void deleteByIds(String ids)
	{
		String sql="delete from BAS_T_ROLE where ROLE_ID in ("+ids+")";
		conn=DBUtil.getConnection();
		PreparedStatement ps;
		
		try {
			ps=conn.prepareStatement(sql);
			ResultSet rs=ps.executeQuery();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 实现分页查询角色信息
	 * @param pageNo 当前页码
	 * @param pageSize 页面大小
	 * @param role   查询信息
	 * @return
	 */
	public Page getRolePage(int pageNo,int pageSize,Object role)
	{		
		return this.find(role, pageNo, pageSize, "BAS_T_ROLE");		
	}
	
}
